package com.zhkj.shixi.tvplayer.PicturePlayer;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.zhkj.shixi.tvplayer.MyMediaPlayer;
import com.zhkj.shixi.tvplayer.R;
import com.zhkj.shixi.tvplayer.adapter.AudioAdapter;
import com.zhkj.shixi.tvplayer.adapter.PlayList;
import com.zhkj.shixi.tvplayer.banner.Banner;
import com.zhkj.shixi.tvplayer.util.USBUtil;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class PicPlayActivity extends Activity  {
    private AlertDialogImpl alertDialog;
    private boolean isBtnPlayOff = false;

    private static final String TAG = "PicPlayActivity";
    private boolean isOver=false;
    private PicPlayerUI picPlayerUI;
    private Banner banner;
    private File mFile;
    private String mCurrentPath;
    private List<File> mPicList;
    private MusicBackGround musicBackGround = new MusicBackGround();
    List<Object> objects;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        objects =USBUtil.getUSBPaths(this);
        setContentView(R.layout.picture_player_layout);
        Intent intent = getIntent();
        Toast.makeText(this, intent.getStringExtra("url"), Toast.LENGTH_SHORT).show();
        mCurrentPath = intent.getStringExtra("url");
        mFile = new File(mCurrentPath);
        mPicList = Arrays.asList(mFile.getParentFile().listFiles(new typeFilter()));
        banner = (Banner) findViewById(R.id.pic_view);
        picPlayerUI = new PicPlayerUI(PicPlayActivity.this);
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                musicBackGround.fileFilterF(findRoot());
                callBack();
            }
        });
        thread.start();
    }

    private String findRoot(){
        for(Object path : objects)
        {
            if(mFile.getAbsolutePath().contains((String)path)){
                return (String)path;
            }
        }
        return "";
    }
    @Override
    public void onBackPressed() {
        if (picPlayerUI.getmPlayListShow().getVisibility()==View.VISIBLE){
            picPlayerUI.getmPlayListShow().setVisibility(View.GONE);
        }else {
            CommonDialog.Builder builder =new CommonDialog.Builder(this);
            builder.setMessage("是否退出播放");
            builder.setTitle("提醒");
            builder.setNegativeButton("退出", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    android.os.Process.killProcess(android.os.Process.myPid());
                }
            });
            builder.setPositiveButton("继续播放", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();

        }

    }
    public boolean isOver() {
        return isOver;
    }

    public void callBack(){
        for(File file: musicBackGround.getFileResult()){
            Log.w("cb",file.getName());
        }
        isOver=true;
        List<PlayList> playList = new ArrayList<>();
        for(File file :musicBackGround.getFileResult())
        {
            PlayList pl = new PlayList(file.getName());
            playList.add(pl);
        }
        AudioAdapter audioAdapter = new AudioAdapter(this,R.layout.item_audioplaylist,R.id.item_audiolist,playList);
        picPlayerUI.getmImageAudioList().setAdapter(audioAdapter);
        picPlayerUI.getmImageAudioList().setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MyMediaPlayer myMediaPlayer = new MyMediaPlayer(PicPlayActivity.this);
                myMediaPlayer.init(musicBackGround.getFileResult().get(position).getAbsolutePath());
                myMediaPlayer.play();
                picPlayerUI.getmPlayListShow().setVisibility(View.GONE);

            }
        });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.i(TAG, "onKeyDown: "+event.getAction());
        switch (keyCode) {
            case KeyEvent.KEYCODE_MENU:
                //打开工具栏
                picPlayerUI.displayTools();
                banner.setScrollables(false);
                picPlayerUI.setfocus();
                picPlayerUI.refreshPicInfo();
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
                if (picPlayerUI.isCtlVisiable()){
                    picPlayerUI.displayTools();
                }else {
                    picPlayerUI.lastPic();
                }
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                if (picPlayerUI.isCtlVisiable()){
                    picPlayerUI.displayTools();
                }else {
                    picPlayerUI.nextPic();
                }
                break;
            case KeyEvent.KEYCODE_DPAD_UP:
                if (picPlayerUI.getSeekBar().hasFocus()){
                    picPlayerUI.seekBarUp();
                    picPlayerUI.displayTools();
                }else if (!picPlayerUI.isCtlVisiable()&&!picPlayerUI.getIsPlaying()){
                    // TODO: 图片旋转
                    Log.e(TAG,"===KEYCODE_DPAD_UP");
                    banner.setType(1);
                    banner.notifyBannerDataChanged();
                }
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                if (picPlayerUI.getSeekBar().hasFocus()){
                    picPlayerUI.seekBarDown();
                    picPlayerUI.displayTools();
                }else if (!picPlayerUI.isCtlVisiable()&&!picPlayerUI.getIsPlaying()) {
                    // TODO: 图片旋转
                    Log.e(TAG,"KEYCODE_DPAD_DOWN");
                    banner.setType(-1);
                    banner.notifyBannerDataChanged();
                }
                break;
            case KeyEvent.KEYCODE_5:

                break;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }


    public File getStartFile(){
        return mFile;
    }
    public File getFile(){
        File file = mPicList.get(picPlayerUI.getmCurrentIndex());
        return file;
    }

    public List<File> getPicList() {
        return mPicList;
    }

    public List<String> getPathList(){
        List<String> pathsList  = new ArrayList<String>();
        for (int i = 0; i < mPicList.size(); i++) {
            pathsList.add(mPicList.get(i).getAbsolutePath());
        }
        return pathsList;
    }

    private static class typeFilter implements FileFilter
    {
        private String fileType;

        private List<String> anyType = new ArrayList<>();
        public typeFilter() {
                final List<String> imageType =  new ArrayList<>();
                imageType.add(".jpg");
                imageType.add(".jpeg");
                imageType.add(".png");
                imageType.add(".gif");
                imageType.add(".webp");
                imageType.add(".ico");
                anyType = imageType;

        }
        public boolean accept(File file) {
            for(int i = 0;i < anyType.size();i++) {
                if(file.getName().toLowerCase().endsWith(anyType.get(i))){
                    return true;
                }
                else if(file.isDirectory()){
                    return false;
                }
            }
            return false;
        }
    }

}
